"""游戏的主要文件"""

from settings import Settings
from stats import GameStats
from scoreboard import Scoreboard
from button import Button
from sound_effect import SoundEffect
import pygame
from spacecraft import Spacecraft
import game_function as gf
from pygame.sprite import Group


def run_game():
    """初始化游戏并创建一个屏幕对象"""
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    background_picture = pygame.image.load('images_source/sky.PNG')
    background_picture = pygame.transform.scale(background_picture,
                                                (settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Aircraft War")

    # 创建Play按钮
    play_button = Button(settings, screen, "Play")

    # 创建一个飞船对象,一个用于存储子弹的编组和一个敌人编组
    spacecraft = Spacecraft(settings, screen)
    bullets = Group()
    enemies = Group()

    # 创建一个用于存储游戏信息的实例，并创建记分牌
    stats = GameStats(settings)
    sb = Scoreboard(settings, screen, stats, bullets, play_button)

    # 创建一个音效对象
    sound = SoundEffect(stats)

    # 创建敌人舰队
    gf.create_fleet(settings, screen, spacecraft, enemies)

    # 开始游戏的主循环
    while True:
        gf.check_events(settings, screen, stats, play_button, sb, sound, spacecraft,
                        enemies, bullets)

        if stats.dictionaries['active']['game_active']:
            spacecraft.adjust_spacecraft_position()
            gf.update_bullets(settings, screen, stats, sb, sound, spacecraft, enemies, bullets)
            gf.update_enemies(settings, screen, stats, sb, sound, spacecraft, enemies, bullets)

        gf.update_screen(settings, screen, stats, sb, spacecraft, enemies,
                         bullets, play_button, background_picture)


run_game()
